package com.franklin.ideaplugin.easytesting.ui.execute;

import cn.hutool.core.collection.CollectionUtil;
import com.franklin.ideaplugin.api.beans.EnumClass;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.psi.PsiParameter;
import com.intellij.ui.components.JBTextField;
import org.apache.commons.compress.utils.Lists;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * 布尔类
 *
 * @author Ye Junhui
 * @since 2023/7/2 12:05
 */
public class BooleanClassParameterFormProvider implements IParameterFormProvider {
    @Override
    public boolean isTarget(@NotNull Object defaultValue) {
        return defaultValue instanceof Boolean;
    }

    @Override
    public ParameterTextComponent getForm(PsiParameter psiParameter, Object defaultValueOfPsiType, Object defaultValue) {
        List<String> booleanList = Lists.newArrayList();
        booleanList.add("true");
        booleanList.add("false");
        if (psiParameter.getType().getCanonicalText().equals(Boolean.class.getCanonicalName())){
            booleanList.add("");
        }
        if (CollectionUtil.isNotEmpty(booleanList)) {
            ComboBox<String> comboBox = new ComboBox<>(booleanList.toArray(new String[0]));
            comboBox.setToolTipText("Select a boolean value");
            int index = booleanList.indexOf(defaultValue.toString());
            if (index > -1) {
                comboBox.setSelectedIndex(index);
            } else {
                comboBox.setSelectedIndex(0);
            }
            return new ParameterTextComponent(comboBox) {
                @Override
                public String getText() {
                    return comboBox.getSelectedItem().toString();
                }
            };
        }
        JBTextField textField = new JBTextField();
        return new ParameterTextComponent(textField) {
            @Override
            public String getText() {
                return textField.getText();
            }
        };
    }
}
